<?php
require_once '../configs/common.tools.php';

$db = new Data();
$memcache = new Cache();
//sync building data
echo "Starting to sync m_building...";
$query = $db->query("select * from m_building");
while ($base_result=$db->fetch_array($query)) {
	$memcache->set("b_".$base_result["id"], $base_result);
}
echo "done<br>";

echo "Starting to sync m_study...";
$query = $db->query("select * from m_study order by id");
$studylist = array();
while ($base_result=$db->fetch_array($query)) {
	$memcache->set("t_".$base_result["id"], $base_result);
	array_push($studylist, $base_result);
}
$memcache->set("t_list", $studylist);
echo "done<br>";

echo "Starting to sync m_soldier...";
$query = $db->query("select * from m_soldier order by s_id");
$soldierlist = array();
$unitlist = array();
while ($base_result = $db->fetch_array($query)) {
	$memcache->set("s_".$base_result["s_id"], $base_result);
	$unitlist[$base_result["s_id"]] = $base_result;
	array_push($soldierlist, $base_result);
}
$memcache->set("s_units", $unitlist);
$memcache->set("s_list", $soldierlist);
echo "done<br>";

echo "Starting to sync m_goods...";
//do clear first
$query = $db->query("select distinct s_stylenum from m_goods");
while ($base_result = $db->fetch_array($query)) {
	$gkey = "i_1_".$base_result["s_stylenum"];
	$memcache->delete($gkey);
}
//now set data
$query = $db->query("select * from m_goods order by s_stylenum asc, s_level asc");
while ($base_result = $db->fetch_array($query)) {
	$memcache->set("i_".$base_result["id"], $base_result);
	
	if ($base_result["forsale"] == 1) {
		$gkey = "i_1_".$base_result["s_stylenum"];
		$itemlist = $memcache->get($gkey);
		if (!$itemlist) {
			$itemlist = array($base_result);
		} else {
			array_push($itemlist, $base_result);
		}
		$memcache->set($gkey, $itemlist);
	}
}
echo "done<br>";

echo "Starting to sync shop...";
//do clear first
$query = $db->query("select distinct cat from shopmall");
while ($base_result=$db->fetch_array($query)) {
	$gkey = "shop_".$base_result["cat"];
	$memcache->delete($gkey);
}
//now set data
$query = $db->query("select * from shopmall order by cat asc, disp asc");
while ($base_result=$db->fetch_array($query)) {
	$gkey = "shop_".$base_result["cat"];
	$itemlist = $memcache->get($gkey);
	if (!$itemlist) {
		$itemlist = array($base_result);
	} else {
		array_push($itemlist, $base_result);
	}
	$memcache->set($gkey, $itemlist);
}
echo "done<br>";

echo "Starting to sync m_gen...";
//do clear first
$query = $db->query("select distinct ga_type,g_rank from m_gen");
while ($base_result = $db->fetch_array($query)) {
	$gkey = "g_".$base_result["ga_type"]."_".$base_result["g_rank"];
	$memcache->delete($gkey);
}
$query = $db->query("select * from m_gen");
while ($base_result=$db->fetch_array($query)) {
	$memcache->set("g_".$base_result["id"], $base_result);
	
	$gkey = "g_".$base_result["ga_type"]."_".$base_result["g_rank"];
	$genlist = $memcache->get($gkey);
	if (!$genlist) {
		$genlist = array($base_result);
	} else {
		array_push($genlist, $base_result);
	}
	$memcache->set($gkey, $genlist);
}
echo "done<br>";

echo "Starting to sync city_res...";
//do clear first
$city_res = $memcache->get("city_res");
if ($city_res) {
	foreach($city_res as $respos) {
		$memcache->delete($respos);
	}
}
$city_res = array();
$query = $db->query("select * from city_res");
while ($base_result=$db->fetch_array($query)) {
	$memcache->set("city_res_".$base_result["area"]."_".$base_result["area_x"]."_".$base_result["area_y"], $base_result);
	array_push($city_res, "city_res_".$base_result["area"]."_".$base_result["area_x"]."_".$base_result["area_y"]);
}
$memcache->set("city_res", $city_res);
echo "done<br>";

echo "Starting to sync city_boss...";
//do clear first
$city_boss = $memcache->get("city_boss");
if ($city_boss) {
	foreach($city_boss as $bosspos) {
		$memcache->delete($bosspos);
	}
}
$city_boss = array();
$query = $db->query("select * from city_boss");
while ($base_result = $db->fetch_array($query)) {
	$memcache->set("city_boss_".$base_result["area"]."_".$base_result["area_x"]."_".$base_result["area_y"], $base_result);
	
	array_push($city_boss, "city_boss_".$base_result["area"]."_".$base_result["area_x"]."_".$base_result["area_y"]);
}
$memcache->set("city_boss", $city_boss);
echo "done<br>";

echo "Starting to sync m_system_task...";
//do clear first
$query = $db->query("select distinct dep from m_system_task");
while ($base_result=$db->fetch_array($query)) {
	if ($base_result["dep"] != 0) {
		$gkey = "q_dep_".$base_result["dep"];
		$memcache->delete($gkey);
	}
}
for ($i = 1; $i <= 10; $i++) {
	$memcache->delete("q_daily_".$i);
}
//now set data
$query = $db->query("select `id`, `task_gas`, `task_alloy`, `task_power`, `task_credit`, `task_exp`, `user_grade`, `dep`, `recursive`, `type`, `ext`, stars from m_system_task order by id");
while ($base_result=$db->fetch_array($query)) {
	$memcache->set("q_".$base_result["id"], $base_result);
	if ($base_result["dep"] != 0) {
		$gkey = "q_dep_".$base_result["dep"];
		
		$itemlist = $memcache->get($gkey);
		if (!$itemlist) {
			$itemlist = array($base_result);
		} else {
			array_push($itemlist, $base_result);
		}
		$memcache->set($gkey, $itemlist);
	}
	if ($base_result["recursive"] == 1) {
		$stars = $base_result["stars"];
		$gkey = "q_daily_".$stars;
		$itemlist = $memcache->get($gkey);
		if (!$itemlist) {
			$itemlist = array($base_result);
		} else {
			array_push($itemlist, $base_result);
		}
		$memcache->set($gkey, $itemlist);
	}
}
$memcache->set("mark_quest_dep_done", 1);
echo "done<br>";

?>